# encoding: utf-8

import os

def main(env):
	string = env["context"]["selection"].encode("utf8").replace("'",r"""'"'"'""") # is there a better way to deal with single quotes for AppleScript?
	suffix = env["meta"]["richSuffix"]
	if suffix.upper() == env["static"]["cwAdium_statusName"].upper(): # status
		os.system("echo 'Adium, %s, %s' | osascript ../cwmenu/menu.scpt" % (env["static"]["cwAdium_statusName"],string.strip()))
		return [("replace","")]
	elif suffix and string: # switch and send a message
		os.system("echo '%s' | osascript switch.scpt" % suffix)
		os.system("echo '%s' | osascript send.scpt" % string)
		return [("append","\n")]
	elif suffix: # switch only
		os.system("echo '%s' | osascript switch.scpt" % suffix)
		return []
	elif string: # send a message only
		os.system("echo '%s' | osascript send.scpt" % string)
		return [("append","\n")]
	else: # launch Adium
		os.system("osascript launch.scpt")
		return []

